package com.novunda.platform.repositories;

import com.novunda.platform.common.repositories.BaseRepository;
import com.novunda.platform.entities.User;
import com.novunda.platform.entities.base.BaseEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;

/**
 * .
 * 功能说明：账号dao
 * @author linfeng
 * @see 2016年9月22日
 * @version 1.0
 * @modify FJ
 */
public interface UserRepository extends BaseRepository<User, Long> {

	/**
	 * .
	 *功能说明：根据账号名查询
	 * @param loginName
	 * @return
	 */
    @Query("from User where loginName = ?1 and delFlag = '" + BaseEntity.DEL_FLAG_NORMAL + "'")
    User findByLoginName(String loginName);

    /**
     * .
     *功能说明：删除账号
     * @param id 账号id
     * @return
     */
    @Modifying
    @Query("update User set uuid = null, delFlag='" + BaseEntity.DEL_FLAG_DELETE + "' where id = ?1")
    int deleteById(Long id);

    /**
     * .
     *功能说明：修改密码
     * @param newPassword 新密码
     * @param id
     * @return
     */
    @Modifying
    @Query("update User set password=?1 where id = ?2")
    int updatePasswordById(String newPassword, Long id);

    /**
     * .
     *功能说明：修改账号信息
     * @param loginIp 登录ip
     * @param loginDate 登录时间
     * @param id 账号id
     * @return
     */
    @Modifying
    @Query("update User set loginIp=?1, loginDate=?2 where id = ?3")
    int updateLoginInfo(String loginIp, Date loginDate, Long id);

    /**
     * .
     *功能说明：根据机构名统计账号数量
     * @param organization 机构名
     * @return
     */
    @Query("select count(id) from User where company.name=?1 and delFlag <> '" + BaseEntity.DEL_FLAG_DELETE + "'")
    int findUserByOrganization(String organization);

    /**
     *  admin查询总的账户数
     * @return 总的账户数
     */
    @Query("select count(id) from User where delFlag <> '" + BaseEntity.DEL_FLAG_DELETE + "'")
    int findUserCountByAdmin();

    /**
     *  机构下，未激活的账号总数
     * @param organization 机构
     * @return 冻结的账号总数
     */
    @Query("select count(id) from User where id !=1 and company.name=?1 and bindStatus=0 and delFlag <> '" + BaseEntity.DEL_FLAG_DELETE + "' and (realName like ?2 or email like ?2 or loginName like ?2 or company.name like ?2)")
    int queryNotActivateUser(String organization,String keyWord);

    /**
     *  admin，未激活的账号总数
     * @param organization 机构
     * @return 冻结的账号总数
     */
    @Query("select count(id) from User where id !=1 and bindStatus=0 and delFlag <> '" + BaseEntity.DEL_FLAG_DELETE + "' and (realName like ?1 or email like ?1 or loginName like ?1 or company.name like ?1)")
    int queryAdminNotActivateUser(String keyWord);
    /**
     * .
     *功能说明：根据机构名统计激活用户数
     * @param organization 机构名
     * @return
     */
    @Query("select count(id) from User where id !=1 and company.name=?1 and bindStatus=1 and delFlag <> '" + BaseEntity.DEL_FLAG_DELETE + "' and (realName like ?2 or email like ?2 or loginName like ?2 or company.name like ?2)")
    int queryActivateUser(String organization,String keyWord);

    /**
     * .
     *功能说明：admin查询激活的用户数
     * @return
     */
    @Query("select count(id) from User where id !=1 and bindStatus=1 and delFlag <> '" + BaseEntity.DEL_FLAG_DELETE + "' and (realName like ?1 or email like ?1 or loginName like ?1 or company.name like ?1)")
    int queryActivateUserByAdmin(String keyWord);

    /**
     * .
     *功能说明：通过token查询账号
     * @param token
     * @return
     */
    @Query("from User where uuid=?1")
    User findUserByToken(String token);
    
    /**
     * .
     *功能说明：通过邮箱和机构id
     * @param email 邮箱
     * @param orgId 机构id
     * @return
     */
    @Query("from User where email=?1 and company.id=?2 and delFlag = '" + BaseEntity.DEL_FLAG_NORMAL + "'")
    User findUserByEmailAndOffice(String email, long orgId);


    @Modifying
    @Query("update User set bindStatus=?1 where id = ?2")
    int updateStatus(int state, Long id);
    
    /**
     * .
     *功能说明：根据id查询账号信息
     * @param id 账号id
     * @return
     */
    @Query("from User where id = ?1 and delFlag = '" + BaseEntity.DEL_FLAG_NORMAL + "'")
    User get(Long id);
}
